package com.chen.lifepic.GrideView;

import java.util.HashMap;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.widget.BaseAdapter;

import com.chen.lifepic.DataInfo.DataInfo;
import com.chen.lifepic.httpList.RequestPicturesURLHelper;
import com.loopj.android.http.JsonHttpResponseHandler;

public class RemoteGrideViewProvider {
	RequestPicturesURLHelper _helper = null;

	JSONObject _jsonObject = null;

	BaseAdapter _adapter = null;

	HashMap<String, DataInfo> _mediaInfosMap = null;

	Vector<String> _indexToKeyMap = null;

	int _index = 0;

	public RemoteGrideViewProvider() {
		_helper = new RequestPicturesURLHelper(_handler);
		_mediaInfosMap = new HashMap<String, DataInfo>();
		_indexToKeyMap = new Vector<String>();
	}

	public void destroy() {
		if (null != _helper) {
			_helper.destroy();
			_helper = null;
		}

		if (null == _mediaInfosMap) {
			_mediaInfosMap.clear();
			_mediaInfosMap = null;
		}

		if (null == _indexToKeyMap) {
			_indexToKeyMap.clear();
			_indexToKeyMap = null;
		}

		_jsonObject = null;
		_adapter = null;
	}

	public void setSelectedIndex(int index) {
		_index = index;
	}

	public int getSelectedIndex() {
		return _index;
	}

	public int getCount() {
		if (null == _indexToKeyMap)
			return 0;

		return _indexToKeyMap.size();
	}

	String locationToKey(int index) {
		if (null == _indexToKeyMap || index < 0
				|| index > _indexToKeyMap.size())
			return null;

		return _indexToKeyMap.get(index);
	}

	public DataInfo getItem(int index) {
		if (null == _mediaInfosMap)
			return null;

		String key = locationToKey(index);

		if (null == key)
			return null;

		return _mediaInfosMap.get(key);
	}

	public String getThumbnailUrl(int index) {
		DataInfo info = getItem(index);

		if (null == info)
			return null;

		return info.thumbnail_url;
	}

	public String getImageUrl(int index) {
		DataInfo info = getItem(index);

		if (null == info)
			return null;

		return info.image_url;
	}

	public void refresh(BaseAdapter adapter, boolean bForce) {
		if (!bForce && null != _jsonObject)
			return;
		_jsonObject = null;
		if (null != _mediaInfosMap) {
			_mediaInfosMap.clear();
		}

		if (null != _indexToKeyMap) {
			_indexToKeyMap.clear();
		}

		_helper.request();
		_adapter = adapter;
	}

	JsonHttpResponseHandler _handler = new JsonHttpResponseHandler() {
		@Override
		public void onSuccess(org.json.JSONObject jsonObject) {
			try {
				_jsonObject = jsonObject;
				CacheObjectTask task = new CacheObjectTask();
				task.execute();
				task = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		};
	};

	public class CacheObjectTask extends AsyncTask<Void, Void, Void> {
		@Override
		protected Void doInBackground(Void... params) {
			cacheJsonObject();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			_adapter.notifyDataSetChanged();
		}
	}

	void cacheJsonObject() {
		if (null == _jsonObject)
			return;

		try {
			JSONArray array = _jsonObject.getJSONArray("data");
			int cout = array.length();
			for (int i = 0; i < cout; i++) {
				JSONObject obj = (JSONObject) array.get(i);

				DataInfo info = DataInfo.convertMediaInfoByJson(obj);
				if (null == info || null == info.serverId
						|| null == info.thumbnail_url || null == info.image_url)
					continue;

				_mediaInfosMap.put(info.serverId, info);
				_indexToKeyMap.add(i, info.serverId);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
